﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace IntelliTouchMix
{
    class Animation
    {
        List <AnimationControl> currentAnimation;
        private int pollPeriod;
        public bool started = false;
        public Animation()
        {
            currentAnimation = new List<AnimationControl>(5);
            pollPeriod = 50; // 50 ms
            Startup();
        }
        public void Startup()
        {
            if (!started)
            {
                new Thread(WorkerProc) { IsBackground = true }
                .Start();
                started = true;
            }
        }
        public void addAnimation(AnimationControl an)
        {
            currentAnimation.Add(an);
        }
        private void WorkerProc()
        {
            return;
            while (true)
            {
                var et = Environment.TickCount;

                // call your methods
                try
                {

                    foreach (AnimationControl anim in currentAnimation)
                    {
                        anim.Invoke(new MethodInvoker(delegate
                        {
                            if (!anim.updateAnimation()) // the animation is done?
                                currentAnimation.Remove(anim);
                        }));
                    }
                }
                catch (Exception e) { }
                // End

                et = Environment.TickCount - et;
                var sleep = pollPeriod - et;
                if (sleep < 0) sleep = 0; // always yield
                Thread.Sleep(sleep);
            }
        }
    }
}
